package com.allm.ai.api.config;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.servlet.config.annotation.CorsRegistry;
import org.springframework.web.servlet.config.annotation.InterceptorRegistry;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurer;

import com.allm.ai.api.interceptor.AuthInterceptor;

import lombok.extern.slf4j.Slf4j;

// @Configuration
@Slf4j
public class WebConfig implements WebMvcConfigurer {
    
    @Autowired
    private AuthInterceptor authInterceptor;
    
    @Override
    public void addInterceptors(InterceptorRegistry registry) {
        registry.addInterceptor(authInterceptor)
                .addPathPatterns("/**") // 拦截所有请求（因为 context-path 已经是 /api）
                .excludePathPatterns(
                    // "/auth/**",           // 认证相关接口
                    // "/ai-models/active",  // 公开的模型列表
                    // "/auth/verify",         // 用户认证验证接口（需要token）
                    "/health"             // 健康检查
                );
        
        log.info("AuthInterceptor 已注册 - 路径模式: /**");
    }
    
    @Override
    public void addCorsMappings(CorsRegistry registry) {
        registry.addMapping("/api/**")
                .allowedOriginPatterns("*")
                .allowedMethods("GET", "POST", "PUT", "DELETE", "OPTIONS")
                .allowedHeaders("*")
                .allowCredentials(true);
    }
}